package com.example.concurrent.chapter16;

/**
 * @author Create By Liuqz
 * @date 2018/11/26 14:16
 *
 * 模拟一个非线程安全的安检口类,旅客(线程)分别手持登机牌和身份证接受工作人员的检查
 */
public class FlightSecurity {

    private int count = 0;

    //登机牌
    private String boardingPass = "null";

    //身份证
    private String idCard = "null";

    public synchronized void pass(String boardingPass, String idCard) {
        this.boardingPass = boardingPass;
        this.idCard = idCard;
        this.count++;
        check();
    }

    private void check() {
        //简单的测试,当登机牌和身份证首字母不相同时则表示检查不通过
        if (boardingPass.charAt(0) != idCard.charAt(0)) {
            throw new RuntimeException("====Exception====" + toString());
        }
    }

    @Override
    public String toString() {
        return "The " + count + " passenagers,boardingPass [" + boardingPass + "],idCard [" + idCard + "]";
    }
}
